package com.kasax.android.arch.handler;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;

public class WeakHandler extends Handler {
    private WeakReference<Activity> activityWeakReference;

    public WeakHandler(Activity activity) {
        super(Looper.myLooper());
        activityWeakReference = new WeakReference<Activity>(activity);
    }

    @Override
    public void handleMessage(@NonNull Message msg) {
        if (activityWeakReference.get() != null) {
            onMessageHandle(msg);
        }
    }

    public void reset() {
        removeCallbacksAndMessages(null);
        activityWeakReference.clear();
        activityWeakReference = null;
    }

    protected void onMessageHandle(Message msg) {

    }
}
